﻿<?php

use yii\helpers\Html;
use yii\helpers\Url;
use backend\widgets\GridView;
use yii\widgets\Breadcrumbs;
use yii\jui\DatePicker;
use yii\widgets\ActiveForm;
use backend\models\service\CateService as Cate;
$this->title = '推文列表';
?>
<div class="RightCenter clear">
    <div class="RightTime">
        <div class="Time">
            <h3>推文列表</h3>
        </div>
    </div>
    <div class="Read_date" style="margin-top:20px;margin-bottom: 5px;height:60px;">
        <ul>
            <li class="Read_LisJ" style="margin-top:12.5px;">
                <div class="Seek">
                    <input type="text" class="form-control AppIN" id="exampleInputAmount" placeholder="搜索书籍名" value="<?php echo isset($select['TweetService']['novel_name']) ? $select['TweetService']['novel_name'] : '';?>">
                    <button type="button" class="btn btn-primary SS">搜索</button>
                </div>
                <div class="SX clear" >
                    筛选:
                    <select id="status">
                        <option value="">全部推文状态</option>
                        <option <?php echo isset($select['TweetService']['status']) && $select['TweetService']['status'] == '0' ? 'selected = "selected"': '';?> value="0">待完善</option>
                        <option <?php echo isset($select['TweetService']['status']) && $select['TweetService']['status'] == 1 ? 'selected = "selected"': '';?> value="1">可推</option>
                        <option <?php echo isset($select['TweetService']['status']) && $select['TweetService']['status'] == 2 ? 'selected = "selected"': '';?> value="2">已推荐</option>
                    </select>
                    <select id="platform">
                        <option value="">全部推广平台</option>
                        <option <?php echo isset($select['TweetService']['platform']) && $select['TweetService']['platform'] == 1 ? 'selected = "selected"': '';?> value="1">微信</option>
                        <option <?php echo isset($select['TweetService']['platform']) && $select['TweetService']['platform'] == 2 ? 'selected = "selected"': '';?> value="2">微博</option>
                        <option <?php echo isset($select['TweetService']['platform']) && $select['TweetService']['platform'] == 3 ? 'selected = "selected"': '';?> value="3">APP</option>
                        <option <?php echo isset($select['TweetService']['platform']) && $select['TweetService']['platform'] == 4 ? 'selected = "selected"': '';?> value="4">其他</option>
                    </select>
                    <select id="op">
                        <option value="">全部合作方</option>
                        <?php foreach($cooperation as $c):?>
                            <option <?php echo isset($select['TweetService']['op_id']) && $c['id'] == $select['TweetService']['op_id'] ? 'selected = "selected"' : '';?> value="<?php echo $c['id'];?>"><?php echo $c['username'];?></option>
                        <?php endforeach;?>
                    </select>
                    <select class="channels">
                        <option value="">全部渠道</option>
                        <?php foreach($channel as $ch):?>
                            <option <?php echo isset($select['TweetService']['channel_id']) && $ch['id'] == $select['TweetService']['channel_id'] ? 'selected = "selected"' : '';?> value="<?php echo $ch['id'];?>"><?php echo $ch['username'];?></option>
                        <?php endforeach;?>
                    </select>
                    <select id="channel" style="display: none;">
                        <option value="">全部渠道</option>
                    </select>
                    <button class="btn btn-primary czS" type="button">重置</button>
                </div>
            </li>
        </ul>
    </div>
    <?=
    GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            [
                'class' => 'yii\grid\CheckboxColumn',
                'name' => 'id',
                'headerOptions' => ['width' => 10],
            ],
            [
                'format' => 'raw',
                'label' => '渠道信息',
                'headerOptions' => ['width' => 80],
                'value' => function($data) {
                    return $data->channel_name . "</br>" . $data->channel_id;
                }
            ],
            [
//                'attribute' => 'tweet_sn',
                'label' => '推广编号',
                'headerOptions' => ['width' => 80],
                'value' => function($data){
                    return $data->tweet_sn;
                }
            ],
            [
//                'attribute' => 'platform',
                'label' => '推广平台',
//                'filter' => Html::activeDropDownList($searchModel, 'platform', ['0' => '请选择', '1' => '微信', '2' => '微博', '3' => 'APP', '4' => '其他']),
                'headerOptions' => ['width' => 50],
                'value' => function($data) {
                    switch ($data->platform) {
                        case '1':
                            return '微信';
                        case '2':
                            return '微博';
                        case '3':
                            return 'APP';
                        case '4':
                            return '其他';
                    }
                }
            ],
            [
//                'attribute' => 'novel_name',
                'label' => '书籍名称',
                'headerOptions' => ['width' => 150],
                'value' => function($data){
                    return $data->novel_name;
                }
            ],
            [
                'label' => '回复关键字',
                'headerOptions' => ['width' => 80],
                'value' => function($data){
                    $res =  backend\models\TweetKeyword::getByTweetId($data->id);
                    return $res['name'];
                }
            ],
            [
                'attribute' => 'create_time',
                'label' => '创建日期',
                'headerOptions' => ['width' => 30],
            ],
            [
                'format' => 'raw',
                'label' => '合作方信息',
                'headerOptions' => ['width' => 80],
                'value' => function($data) {
            return $data->op_name . "</br>" . $data->op_id;
        }
            ],
            [
                'label' => '推文状态',
//                'filter' => Html::activeDropDownList($searchModel, 'status', ['' => '请选择', '0' => '待完善', '1' => '可推', '2' => '已推荐']),
                'headerOptions' => ['width' => 55],
                'value' => function($data) {
                    switch ($data->status) {
                        case '0':
                            return '待完善';
                        case '1':
                            return '可推';
                        case '2':
                            return '已推荐';
                    }
                }
            ],
            [
                'class' => \yii\grid\ActionColumn::className(),
                'header' => '操作',
                'headerOptions' => ['width' => 180],
                'contentOptions' => ['class' => 'action-link'],
                'template' => '{view} {update} {del}',
                'buttons' => [
                    'view' => function ($url, $model, $key) {
                        return Html::a('推文预览', "http://".$_SERVER['HTTP_HOST'].'/tpl/'.$model->channel_name.'/html/'.$model->id.'.html') . " ";
                    },
                    'update' => function ($url, $model, $key) {
                        return Html::a('编辑', $url) . " ";
                    },
                             'del' => function ($url, $model, $key) {
                                return Html::a('删除', $url, [
                                    'data' => [
                                        'confirm' => '确定/删除？',
                                        'method' => 'post',
                                    ],
                                ]);
                             }
                        ],
                    ],
                ],
            ]);
            ?>

</div>
<script>
    $(".czS").click(function(){
        location.href="/tweet/index";
    });
    
    $(".SS").click(function(){
        var status = $("#status").val();
        var platform = $("#platform").val();
        var op = $("#op").val();
        var channel = $("#channel").val();
        if(channel == ''){
            channel = $(".channels").val();
        }
        var name = $("#exampleInputAmount").val();
        if(status == '' && platform == '' && op == '' && channel == '' && name == ''){
            return false;
        }
        location.href="/tweet/index?TweetService[status]="+status+"&TweetService[platform]="+platform+"&TweetService[op_id]="+op+"&TweetService[channel_id]="+channel+"&TweetService[novel_name]="+name;
    });
    
    $("#op").change(function(){
        var op_id = $(this).val();
        if(op_id == ''){
            $(".channels").show();
            $("#channel").hide();
        }else{
            $(".channels").hide();
            get(op_id);
        }
    });
    var cid = '<?php echo isset($select['TweetService']['channel_id']) ? $select['TweetService']['channel_id'] : '';?>';
    function get(id){
        $("#channel").show();
        $.ajax({
            type:"post",
            url:"/admin/get-channel-nums",
            data:{pid:id,type:3},
            success:function(msg){
                $("#channel").empty();
                var str = "<option value=''>全部渠道</option>"+msg;
                $("#channel").append(str);
                $("#channel").find("option[value='"+cid+"']").attr('selected','selected')
            }
        });
    }
    
    $(function(){
        var op_id = $("#op").val();
        if(op_id == ''){
            $(".channels").show();
            $("#channel").hide();
        }else{
            $(".channels").hide();
            get(op_id);
        }
    });
</script>
